using System;
using System.IO.Ports;

class Sender
{
    static SerialPort _serialPort;

    static void Main()
    {
        // 1. 配置串口参数（根据实际修改）
        _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
        {
            Handshake = Handshake.None,
            WriteTimeout = 500
        };

        try
        {
            _serialPort.Open();
            Console.WriteLine("甲方已启动，输入消息发送给乙方（输入 'exit' 退出）...");

            // 2. 循环发送消息
            while (true)
            {
                string input = Console.ReadLine();
                if (input.ToLower() == "exit") break;

                // 添加发送时间戳
                string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string formattedMessage = $"[SENT {timestamp}] {input}";

                // 发送数据并显示
                _serialPort.WriteLine(formattedMessage);
                Console.WriteLine($"甲方显示: {formattedMessage}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
        finally
        {
            if (_serialPort.IsOpen) _serialPort.Close();
        }
    }
}